//
//  SignInVC.m
//  YiShangPin
//
//  Created by yishangpin2 on 15/3/23.
//  Copyright (c) 2015年 yishangpin2. All rights reserved.
//

#import "SignInVC.h"
#import "AppDelegate.h"

@interface SignInVC (){

    AppDelegate *delegate;
    
}

@end

@implementation SignInVC

- (void)viewDidLoad {
    [super viewDidLoad];
    delegate = [[UIApplication sharedApplication]delegate];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewWillDisappear:(BOOL)animated{
    self.navigationController.navigationBarHidden = YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - 登陆结束后 ，转换根控制器

- (IBAction)Sign:(id)sender {
    if ([_emailText.text isEqualToString:@""] || [_passwordText.text isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Please fill in all the fields" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }
    else {
        //拼接模块和方法名
        NSURL *url = [NSURL URLWithString:[delegate.path_URL stringByAppendingString:@"UserInfo/matchUser"]];
        
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
        
        [request setHTTPMethod:@"POST"];
        NSString *string = [NSString stringWithFormat:@"email=%@&password=%@",_emailText.text,_passwordText.text];
        NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
        [request setHTTPBody:data];
        [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            
            NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            
            [self performSelectorOnMainThread:@selector(refresDate:) withObject:str waitUntilDone:YES];
            
        }];

    }
}

- (void)refresDate:(NSString *)sender{
    if ([sender isEqualToString:@"登陆成功"]) {
        //登陆成功跳转到下一页面
        [delegate SetRootViewsideview];
        //    [self dismissModalViewControllerAnimated:YES];
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    else {
        NSLog(@"%@,%@,%@",sender,_emailText.text,_passwordText.text);
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Email or Password is incorrect" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }
}

@end
